package com.party.admin.biz.mall;

import com.party.admin.biz.file.FileBizService;
import com.party.admin.web.dto.input.mall.MallStoreInput;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallStore;
import com.party.core.model.mall.MallStoreOutput;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.mall.IMallStoreService;
import com.party.core.service.mall.biz.StoreResourceBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.utils.MyBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺业务处理
 * Created by yifeng
 *
 * @date 2018/4/4 0004
 * @time 下午 16:31
 */
@Service
public class MallStoreBizService {

    private static Logger logger = LoggerFactory.getLogger(MallStoreBizService.class);

    @Value("#{party['shop.url.protocol']}")
    private String shopUrl;

    @Autowired
    private StoreResourceBizService storeResourceBizService;
    @Autowired
    private IMallStoreService mallStoreService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private FileBizService fileBizService;

    /**
     * 保存店铺
     *
     * @param mallStoreInput 店铺信息
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveStoreBiz(MallStoreInput mallStoreInput) {
        String id = mallStoreInput.getId();
        if (StringUtils.isEmpty(id)) {
            id = mallStoreService.insert(mallStoreInput);
        } else {
            mallStoreService.update(mallStoreInput);
        }
        // 生成店铺二维码
        try {
            String memberId = mallStoreInput.getCreateBy();
            if (StringUtils.isEmpty(memberId)) {
                MemberGroup txz = memberGroupService.getTXZ();
                memberId = txz.getId();
            }
            String filePath = fileBizService.getUploadCIPath(memberId, "shop");
            String fileName = "home_" + id;
            String content = shopUrl + "home?storeId=" + id;
            fileBizService.uploadCloudOut(fileName, filePath, content, memberId, false);
        } catch (Exception e) {
            logger.error("生成店铺主页二维码异常", e);
        }
        storeResourceBizService.saveResourceBiz(mallStoreInput.getResources(), mallStoreInput.getId());
    }

    /**
     * 编辑
     *
     * @return
     */
    public MallStoreOutput getStoreView() {
        MallStoreOutput storeOutput = new MallStoreOutput();
        MallStore mallStore = mallStoreService.getUnique();
        if (mallStore == null) {
            return storeOutput;
        }
        try {
            MyBeanUtils.copyBeanNotNull2Bean(mallStore, storeOutput);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        storeResourceBizService.getStoreResource(storeOutput);
        return storeOutput;
    }
}
